package avicit.bdp.dcs.detail.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.dto.ProcessInstanceDto;
import avicit.bdp.common.dto.TaskInstanceDto;
import avicit.bdp.common.utils.DateUtils;
import avicit.bdp.common.utils.enums.JobType;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dcs.detail.dao.TaskTableLogDao;
import avicit.bdp.dcs.detail.dto.TaskTableLogDTO;
import avicit.bdp.dcs.job.dao.DcsJobDao;
import avicit.bdp.dcs.task.dto.TaskDTO;
import avicit.platform6.commons.utils.ComUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2023-03-28 11:03
 * @类说明：
 * @修改记录：
 */
@Service
@Transactional
public class TaskTableLogService extends BaseService<TaskTableLogDao, TaskTableLogDTO> {

  private static final Logger logger = LoggerFactory.getLogger(TaskTableLogService.class);

  @Autowired private TaskTableLogDao taskTableLogDao;
  @Autowired private DcsJobDao jobService;

  /**
   * 保存创建表日志
   *
   * @param task
   * @param tableLog
   * @throws Exception
   */
  public void saveTableLog(TaskDTO task, String tableLog) {
    try {
      tableLog = DateUtils.dateToString(new Date()) + " " + tableLog;

      // 查询任务实例
      ProcessInstanceDto processInstanceDto = jobService.getProcessInstanceByProcessDefinitionId(task.getJobId());
      if (processInstanceDto == null) {
        logger.warn("processInstance不存在,流程定义id={}", task.getJobId());
        return;
      }
      String processInstanceId = processInstanceDto.getId();

      String name = task.getId() + Constants.SUBTRACT_STRING + task.getSrcTableName();

      if(task.getJobType() == JobType.STREAM.getCode()){
        name = task.getId() + Constants.SUBTRACT_STRING + "createTable";
      }else if(task.getJobType() == JobType.API_HTTP.getCode()){
        name = task.getId() + Constants.SUBTRACT_STRING + "createTableNode";
      }
      TaskInstanceDto taskInstanceDto = taskTableLogDao.getTaskInstance(task.getJobId(), processInstanceId, name);
      if (taskInstanceDto == null) {
        logger.warn("taskInstance不存在,流程定义id={},processInstanceId={},name={}", task.getJobId(), processInstanceId, name);
        return;
      }
      String taskInstanceId = taskInstanceDto.getId();

      // 保存创建表日志
      TaskTableLogDTO tableLogDTO = taskTableLogDao.selectTableLog(processInstanceId, taskInstanceId);
      if (tableLogDTO == null) {
        TaskTableLogDTO taskTableLogDTO = new TaskTableLogDTO();
        taskTableLogDTO.setId(ComUtil.getId());
        taskTableLogDTO.setProcessInstanceId(processInstanceId);
        taskTableLogDTO.setTaskInstanceId(taskInstanceId);
        taskTableLogDTO.setTableLog(tableLog);
        taskTableLogDTO.setCreationDate(new Date());
        taskTableLogDao.insert(taskTableLogDTO);
      } else {
        tableLog = tableLogDTO.getTableLog() + "\n" + tableLog;
        taskTableLogDao.updateTableLog(tableLogDTO.getId(), tableLog);
      }

    } catch (Exception e) {
      logger.error("保存创建表日志失败,taskId={}", task.getId());
      logger.error(e.getMessage());
    }
  }

  public TaskTableLogDTO selectTableLog(String processInstanceId, String taskInstanceId) {
    return taskTableLogDao.selectTableLog(processInstanceId, taskInstanceId);
  }

  public TaskInstanceDto getLastestTaskInstanceByProcessDefinitionId(String processDefinitionId) {
    return taskTableLogDao.getLastestTaskInstanceByProcessDefinitionId(processDefinitionId);
  }

  public TaskInstanceDto getTaskInstanceByProcessInstanceId(
      String processInstanceId, String taskId) {
    return taskTableLogDao.getTaskInstanceByProcessInstanceId(processInstanceId, taskId);
  }
}
